<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/01/11
 * Time: 09:39 PM
 */

namespace Home\Controller;


use Think\Controller;

class RegisterController extends Controller
{
        function index(){
            $this->display('Web/register');
        }


    public function adduser(){
        $data['username']=$_POST["username"];
        $data['password']=md5($_POST["password"]);
        $data['nickname']= $data['username'];
        $data['check']='0';
        $data['type']=0;
        if (M('userinfo')->where('username= "'. $data['username'].'"')->find())
        {
            //已经注册过了
            $this->assign("code",2);
        }else {
            $user = M("userinfo");
            $id = $user->add($data);
            $check = md5($id + 'check');
            $user->check = $check;
            $user->where('id=' . $id)->save();

            $this->send_mail($data['username'], $check);
            //注册
            $this->assign("code", 1);
        }
        $this->display("Web/register");
    }

    function check(){
      $Token = $_GET['accessToken'];
       $user = M('userinfo')->where('tp_userinfo.check="'.$Token.'"')->find();

        if ($user)
        {
            $user['type']=1;
            M('userinfo')->save($user);
            //验证成功
            $this->assign("code",1);
        }else{
            //验证失败
            $this->assign("code",2);
        }
        $this->display("Web/check");

    }

    function send_mail($mailaddress,$token){
        vendor('PHPMailer.class#phpmailer'); //从PHPMailer目录导class.phpmailer.php类文件
        $mail = new \PHPMailer;
        $webname=C('WEB_NAME');
//$mail->SMTPDebug = 2;                               // Enable verbose debug output

        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'smtp.163.com';  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
//    $mail->SMTPAuth = false;                               // Enable SMTP authentication
        $mail->Username = 'zgscwjm_vip@163.com';                 // SMTP username
        $mail->Password = 'wjm6250560';                           // SMTP password
//    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 25;                                    // TCP port to connect to

        $mail->setFrom('zgscwjm_vip@163.com', $webname);
        $mail->addAddress($mailaddress);

        $mail->isHTML(true);                                  // Set email format to HTML
        $link='http://'.$_SERVER['HTTP_HOST'].'/taobao_3dchat/index.php/register/check?accessToken='.$token;

        $str = <<<html
            您好！<p></p>
            感谢您在 $webname 注册帐户！<p></p>
            帐户需要激活才能使用，赶紧激活成为 $webname 的正式一员吧:)<p></p>
            点击下面的链接立即激活帐户(或将网址复制到浏览器中打开):<p></p>
            $link
html;
        $mail->Subject = $webname.'注册验证';
        $mail->Body    = $str;

        return $mail->send();
    }


}